package com.yaodian100.core.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlRegexUtil {
	// onEvent | <script>xxx</script> | <script xxx/> | <object> | <iframe>|<html>|<head>|<body> |<form>
	private final static String REMOVE_REG = "on[a-z]*=\"(.*?)\"|on[a-z]*='(.*?)'|"
			+ "<script(.*?)>(.*?)</script>|<script(.*?)/>|" + "<object(.*?)>(.*?)</object>|<object(.*?)/>|"
			+ "<iframe(.*?)>(.*?)</iframe>|<iframe(.*?)/>|" + "<html(.*?)>|</html>|" + "<head(.*?)>|</head>|"
			+ "<body(.*?)>|</body>|" + "<form(.*?)>|</form>";

	public static String secureMatch(String source) {
		StringBuffer sb = new StringBuffer();
		if (StringUtils.isNotBlank(source)) {
			Pattern pattern = Pattern.compile(REMOVE_REG, Pattern.CASE_INSENSITIVE);
			Matcher matcher = pattern.matcher(source);
			boolean result1 = matcher.find();
			while (result1) {
				System.out.println("match:" + source.substring(matcher.start(), matcher.end())); // NOPMD
				matcher.appendReplacement(sb, "");
				result1 = matcher.find();
			}
			matcher.appendTail(sb);
		}
		return sb.toString();
	}

}